﻿$(function(){//延迟加载  	
	$('#xhrow').load('dvfirmsec',function(){
		$.getScript('/xasj/js/component/dvmodelsec.js');
	});
	
	$('#jfrow').load('showjifangsec',function(){
		$.getScript('/xasj/js/component/jifangsec.js');
	});
	
	$('#failure').combobox();

	$('#lastedminbegin').numberbox({
		
	});
	
	$('#lastedminend').numberbox();
	
	$('#dvbel').combobox();
	
	$('#ceshi').click(function(){
		alert($('#jifang').combobox('getValue'));
	});
	
	$('#search').click(function(){
			var danxuan='',fuxuan='',shuzi='',zifu='',riqi='';
			var fsl='[[';
			var jId='';
			var jcode='';			
			jId=$('#dajuxiang').val();
			jcode='djxId';
			var dmId='';
			var dmcode='';
			var riqi='';
			if($('#dajuxiang').combobox('getValue')!=''){
				jId=$('#dajuxiang').combobox('getValue');
				jcode='djxId';
				if($('#juxiang').combobox('getValue')!='请选择'&&$('#juxiang').combobox('getValue')!=''){
					jId=$('#juxiang').combobox('getValue');
					jcode='jxId';
					if($('#jifang').combobox('getValue')!='请选择'&&$('#jifang').combobox('getValue')!=''){
						jId=$('#jifang').combobox('getValue');
						jcode='jfId';
					}
				}
				danxuan=danxuan+jcode+','+jId;
			}
			
			if($('#dvfirm').combobox('getValue')!=''){
				dmId=$('#dvfirm').combobox('getValue');
				dmcode='dvfirmId';
				danxuan=danxuan+':';
				if($('#dvmodel').combobox('getValue')!='请选择'&&$('#dvmodel').combobox('getValue')!=''){
					dmId=$('#dvmodel').combobox('getValue');
					dmcode='dvmodelId';
				}
				danxuan=danxuan+dmcode+','+dmId;
			}
			
			var failuretypeId=$('#failuretype').combobox('getValue');
			var failureId=$('#failure').combobox('getValue');
			if(failureId!=''){
				if(danxuan!=''){
					danxuan=danxuan+':failureId,'+failureId
				}else{
					danxuan='failureId,'+failureId
				}
			}else if(failuretypeId!=''){
				if(danxuan!=''){
					danxuan=danxuan+':failuretypeId,'+failuretypeId
				}else{
					danxuan='failuretypeId,'+failuretypeId
				}
			}			
//			dmId=$('#dvfirm')
//			danxuan=danxuan+':'+jcode+','+jId;
			if($('#dvbel').combobox('getValue')!=''){
				if(danxuan!='')
					danxuan=danxuan+':'+'dvbelong,'+$('#dvbel').combobox('getValue');
				else
					danxuan='dvbelong,'+$('#dvbel').combobox('getValue');
			}
			if($('#dvname').val()!=''){//第一个字符
				zifu='dvname,'+$('#dvname').val();
			}
			if($('#dvip').val()!=''){
				if(zifu!='')
					zifu=zifu+':dvip,'+$('#dvip').val();
				else
					zifu='dvip,'+$('#dvip').val();
			}
			var smin=$('#lastedminbegin').val();
			var smax=$('#lastedminend').val();
			if(smin==''&&smax==''){
				
			}else{
				if(smin=='')
					smin='0';
				shuzi='lastedmin,'+smin+','+smax;
			}
			var begintime=$('#begintime').datetimebox('getValue');
			var endtime=$('#endtime').datetimebox('getValue');
			if(begintime!=''||endtime!=''){
				if(begintime=='')
					begintime='0';
				riqi='begintime,'+begintime+','+endtime;
			}
			$('input[name=fshow]:checked').each(function(index){
				if($(this).val()=='begintime'||$(this).val()=='endtime'){
					fsl=fsl+"{field:'"+$(this).val()+"',title:'"+$(this).data('fname')+"',formatter:function(value,row,index){" +
					"if(value!=null&&value!=''){" +
					"var date = new Date(value);" +
					"Y = date.getFullYear() + '-';" +
					"M = (date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1) : date.getMonth()+1) + '-';" +
					"D = (date.getDate()< 10 ? '0'+(date.getDate()) : date.getDate()) + ' ';" +
					"h =(date.getHours()< 10 ? '0'+(date.getHours()) : date.getHours()) + ':';" +
					"m =(date.getMinutes()< 10 ? '0'+(date.getMinutes()) : date.getMinutes()) + ':';" +
					"s =(date.getSeconds()< 10 ? '0'+(date.getSeconds()) : date.getSeconds());" +
					" return Y+M+D+h+m+s;}else{return '';}},width"+":'100px'}"+",";
				}else{
					fsl=fsl+"{field:'"+$(this).val()+"',title:'"+$(this).data('fname')+"',width"+":'100px'}"+",";
				}		
			});
			fsl=fsl.substring(0,fsl.length-1);
			fsl=fsl+']]';
//			alert(fsl);
			var obj =eval(fsl);		
			$('#searchres').datagrid({
				width:'auto',
				height:'auto',
				//striped:true,		
				singleSelect:true,
				url:'getresofdvfailure',
				method:'POST',
				queryParams:{
					fuxuan:fuxuan,
					zf:zifu,
					danxuan:danxuan,
					shuzi:shuzi,
					riqi:riqi
				},
				pagination:true,
				rownumbers:true,
				height:'400px',			
				columns:obj,
				onLoadSuccess:function(){
					var pager = $('#searchres').datagrid('getPager');	// get the pager of datagrid
					pager.pagination({
						buttons:[{
							iconCls:'icon-search',
							text:'设备详细拓扑',
							width:'180px',
							handler:function(){
								var secrow=$('#searchres').datagrid('getSelected');
//								alert(secrow);
								if(secrow!=null){				
									var dvip=secrow.dvip;
									var dvname=secrow.dvname;
									var dvId=secrow.dvId;
									var dvbelong=secrow.dvbelong;
									parent.addTab(dvname+'('+dvip+')','tojxtp?dvIds='+dvbelong+','+dvId);	
								}
							}
						},{
							iconCls:'icon-search',
							text:'设备详细信息',
							width:'180px',
							handler:function(){
								var secrow=$('#searchres').datagrid('getSelected');
								var dvname=secrow.dvname;
								var dvbelong=secrow.dvbelong;
								var dvId=secrow.dvId;
								parent.addTab(dvname,'toeditdv?dvId='+dvId+'&dvbelong='+dvbelong);
							}
						},{
							iconCls:'icon-redo',
							text:'导出当前数据',
							width:'180px',
							handler:function(){
								 var rows = $('#searchres').datagrid('getRows');  
						            var columns = $("#searchres").datagrid("options").columns[0];  
						            var oXL = new ActiveXObject("Excel.Application"); //创建AX对象excel   
						            var oWB = oXL.Workbooks.Add(); //获取workbook对象   
						            var oSheet = oWB.ActiveSheet; //激活当前sheet  
						            //设置工作薄名称  
						            oSheet.name = "导出Excel报表";  
						            //设置表头  
						            for (var i = 0; i < columns.length; i++) {  
						                oSheet.Cells(1, i+1).value = columns[i].title;  
						            }  
						            //设置内容部分  
						            for (var i = 0; i < rows.length; i++) {  
						                //动态获取每一行每一列的数据值  
						                for (var j = 0; j < columns.length; j++) {                 
						                    oSheet.Cells(i + 2, j+1).value = rows[i][columns[j].field];  
						                }     
						            }                
						            oXL.Visible = true; //设置excel可见属性  
							}
						},{
							iconCls:'icon-redo',
							text:'导出全部数据',
							width:'180px',
							handler:function(){
								 var rows = $('#searchres').datagrid('getRows');  
						            var columns = $("#curfailurelist").datagrid("options").columns[0];  
						            var oXL = new ActiveXObject("Excel.Application"); //创建AX对象excel   
						            var oWB = oXL.Workbooks.Add(); //获取workbook对象   
						            var oSheet = oWB.ActiveSheet; //激活当前sheet  
						            //设置工作薄名称  
						            oSheet.name = "导出Excel报表";  
						            //设置表头  
						            for (var i = 0; i < columns.length; i++) {  
						                oSheet.Cells(1, i+1).value = columns[i].title;  
						            }  
						            //设置内容部分  
						            for (var i = 0; i < rows.length; i++) {  
						                //动态获取每一行每一列的数据值  
						                for (var j = 0; j < columns.length; j++) {                 
						                    oSheet.Cells(i + 2, j+1).value = rows[i][columns[j].field];  
						                }     
						            }                
						            oXL.Visible = true; //设置excel可见属性  
							}
						}]
					});
				}
			});
	});
	$('#failure').combobox({
		disabled:true
	});
	$('#failuretype').combobox({
		onChange:function(newValue,oldValue){
			if(newValue==''){
				$('#failure').combobox({
					disabled:true
				});
			}else{	
				$('#failure').combobox({
					valueField:'failureId',
					textField:'failurename',
					disabled:false,
					url:"getfailurebytypeId?failuretypeId="+newValue
				});
			}
		}
	});
    $.extend($.fn.validatebox.defaults.rules, {    
    	 ip : {// 验证IP地址  
             validator : function(value) {  
                 var reg = /^((1?\d?\d|(2([0-4]\d|5[0-5])))\.){3}(1?\d?\d|(2([0-4]\d|5[0-5])))$/ ;  
                 return reg.test(value);  
             	},  
             message : 'IP地址格式不正确'  
    	 	}
    });
});